import 'dart:io';

import 'package:dartkit/dartkit.dart';
import 'package:test/test.dart';

void main() {
  final File testFile = File(".map_writer_test.json");
  final MapWriter writer = FileWriterMap(testFile);

  tearDown(() => testFile.deleteIf());

  test("基本数据类型", () {
    // 放入字符串、int、bool、double都顺利运行
    writer.input("string", "string");
    expect(writer.getOne<String>("string"), "string");
    writer.input("int", 2);
    expect(writer.getOne<int>("int"), 2);
    writer.input("double", 2.0);
    expect(writer.getOne<double>("double"), 2.0);
    writer.input("bool", true);
    expect(writer.getOne<bool>("bool"), true);
  });

  test("清空", () {
    // 放入数据后清空，key也不存在了
    expect(writer.containsKey("empty"), isFalse);
    writer.input("empty", "任意数据");
    expect(writer.containsKey("empty"), isTrue);
    writer.input("empty", null);
    expect(writer.containsKey("empty"), isFalse);
  });

  test("Rawable对象存储", () {
    // 将People存入，再使用相同的key提取，可以恢复原来的数据
    writer.input("rawable", pseudoThree);
    final people = writer.getObject<PseudoPeople>("rawable",
        unzip: (raw) => PseudoPeople.fromRaw(raw));
    expect(people.name, "张三");
  });

  test("List存储", () {
    // 设置2个People存入，读取时能恢复为List，并且名字正确
    writer.input("list", pseudoPeople);
    List<PseudoPeople> peoplesRead = writer.getList<PseudoPeople>("list",
        unzip: (raw) => PseudoPeople.fromRaw(raw));
    expect(peoplesRead, hasLength(3));
    expect(peoplesRead[1].name, "李四");
  });

  test("inputIf", () {
    // 在一开始可以放入，但是当已经有数值之后，就无法修改了
    writer.inputIf("if", 10);
    expect(writer.getOne<int>("if"), 10);
    writer.inputIf("if", 20);
    expect(writer.getOne<int>("if"), 10);
  });
}
